﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="criarAnuncio.aspx.cs" Inherits="LI4.criarAnuncio" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">


    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=abcdefg&sensor=true_or_false"
            type="text/javascript"></script>
    <script type="text/javascript">

    var map;
    var geocoder;
    var address;
    var marker;
    
    function initialize() {
        
       try{
          if (GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById("map_canvas"));
            geocoder = new GClientGeocoder();
            address = document.getElementById("Text1").value;
            showAddress(address);
           
            
          } 
          GEvent.addListener(map,"click", function(overlay, latlng) {     
          if (latlng) { 
            document.getElementById("click_lat").value = latlng.y;
            document.getElementById("click_long").value = latlng.x;
            marker.setLatLng(latlng);
            map.addOverlay(marker);
          }
            }); 
          
             
           /*GEvent.addListener(marker, "dragstart", function() {
          map.closeInfoWindow();
          });*/
          

       /* GEvent.addListener(marker, "dragend", function() {
          marker.openInfoWindowHtml("Just bouncing afloat...");
          document.getElementById("click_lat").value = marker.getLatLng().y;
            document.getElementById("click_float").value = marker.getLatLng().x;
           });*/
        }
        catch(err)
        {
            alert("Não é possivel estabelecer a ligaçao com o google maps.\n Defina a posição no mapa posteriormente.");
        }
    }
    
     

    
    
 
    
    
    //ler do textbox html e passar o argumento pra procurar no google maps
   function  readFormShow(){
        address = document.getElementById("address").value;
        showAddress(address);
           //document.getElementById("click_lat").value = parseFloat(address);
    }
    
    //funçao que atravez do nome mostra no mapa
    function showAddress(address) {
     geocoder.setBaseCountryCode("PT");
      geocoder.getLatLng(
        address,
        function(point) {
          if (!point) {//procurar apenas pelo nome de localidade
            var add = address.split(",",2);
            geocoder.getLatLng( add[1], function(point) {
                if (!point) {
                    alert(address + " not found");
                } else {
                    map.setCenter(point, 13);
                    map.setUIToDefault();
                    marker = new GMarker(point, {draggable: true});
                    map.addOverlay(marker);
                    
                    document.getElementById("click_lat").value = marker.getLatLng().y;
                    document.getElementById("click_long").value = marker.getLatLng().x;
               }
        });
            //alert(address + " not found");
          } else {
            map.setCenter(point, 13);
            map.setUIToDefault();
            marker = new GMarker(point, {draggable: true});
            map.addOverlay(marker);
            
            document.getElementById("click_lat").value = marker.getLatLng().y;
            document.getElementById("click_long").value = marker.getLatLng().x;
           }
        });
   }
    
    /*function getAddress(overlay, latlng) {
      if (latlng != null) {
        address = latlng;
        geocoder.getLocations(latlng, showAddress);
      }
    }*/

   

    </script>





    <style type="text/css">
        .style1
        {
            width: 50px;
        }
        .style2
        {
            width: 502px;
        }
        .style3
        {
            width: 302px;
        }
        #Text1
        {
            width: 142px;
        }
    </style>

</head>

<body onload="initialize()" onunload="GUnload()">
    <form id="form1" runat="server">
    <div class="style1">
    <table style="width: 800px"><tr>
        <td class="style3">
    
        Criar Anuncio<br />
        <br />
        Distrito:
        <asp:DropDownList ID="distritosLB" runat="server" AutoPostBack="True" 
            style="margin-left: 36px">
        </asp:DropDownList>
        <br />
        <br />
        Localidade:
        <asp:DropDownList ID="localidadeLB" runat="server" style="margin-left: 12px">
        </asp:DropDownList>
        <br />
        <br />
        Morada:&nbsp;
        <input id="Text1" type="text"  runat="server"/>&nbsp; &nbsp;
            <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Mapa" />
            <br />
        <br />
        Código Postal:
        <asp:TextBox ID="codpostalTB" runat="server" MaxLength="4" Width="62px"></asp:TextBox>
        &nbsp;&nbsp;
        <asp:TextBox ID="codpostalTB1" runat="server" MaxLength="3" Width="43px"></asp:TextBox>
        <br />
        <br />
        Área:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;       <asp:DropDownList ID="areaLB" runat="server">
        </asp:DropDownList>
        <br />
        <br />
        Descrição:<br />
&nbsp;<asp:TextBox ID="descricaoTB" runat="server" Height="79px" TextMode="MultiLine" 
            MaxLength="500"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
            style="margin-left: 123px" Text="Criar" />
        <br />
        <br />
        <br />
        </td>
        <td class="style2">
    
            <div id="map_canvas" style="height: 443px; width: 450px;"> </div>
            
            <input type="hidden"   id="click_lat"	onclick="this.blur()" runat="server"/>&nbsp;

	        <input type="hidden"  id="click_long"	onclick="this.blur()" runat="server"/>&nbsp;
    
    
        </td>
    
    
        </tr></div>
    </table>
    </form>
<p>
    &nbsp;</p>
</body>
</html>
